package utils;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import dao.TestSuiteDAO;

import entity.TestSuite;

public class TestSuiteLazyList extends LazyDataModel<TestSuite> {

	private static final long serialVersionUID = 1L;
	private List<TestSuite> testSuites;
	private TestSuiteDAO suiteDAO;

	@Override
	public List<TestSuite> load(int startingAt, int maxPerPage,
			String sortField, SortOrder sortOrder, Map<String, String> filters) {
		// TODO Auto-generated method stub
		try {

			suiteDAO = new TestSuiteDAO();

			suiteDAO.beginTransaction();

			// with datatable pagination limits
			testSuites = suiteDAO.findProjetcs(startingAt, maxPerPage);

			// If there is no player created yet, we will create 100!!

		} catch (Exception e) {
			e.printStackTrace();
		}

		// set the total of players
		if (getRowCount() <= 0) {
			setRowCount(suiteDAO.countTestSuiteTotal());
		}

		// set the page dize
		setPageSize(maxPerPage);

		return testSuites;
	}

	@Override
	public Object getRowKey(TestSuite testSuite) {
		return testSuite.getTestSId();
	}

	@Override
	public TestSuite getRowData(String playerId) {
		Integer id = Integer.valueOf(playerId);

		for (TestSuite testSuite : testSuites) {
			if (id.equals(testSuite.getTestSId())) {
				return testSuite;
			}
		}

		return null;
	}
}
